package cn.fx.framework.utils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class LocalDateTimeUtil {

    /**
     * 将时间间隔转换为“x天x小时x分x秒”格式
     * @param duration 时间间隔
     * @return x天x小时x分x秒
     * @author fxcn
     * @since 2024-9-24
     */
    public static String durationToDayHourMinuteSecond(Duration duration) {
        StringBuffer sb = new StringBuffer();
        long days = duration.toDays();
        if (days > 0) {
            sb.append(days).append("天");
        }
        long hours = duration.toHours();
        if (hours > 0) {
            sb.append(hours % 24).append("小时");
        }
        long minutes = duration.toMinutes();
        if (minutes > 0) {
            sb.append(minutes % 60).append("分");
        }
        long seconds = duration.getSeconds();
        sb.append(seconds % 60).append("秒");
        return sb.toString();
    }

    /**
     * 将Date数据转换为LocalDateTime类型
     * @param date 输入Date数据
     * @return  LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 将LocalDateTime数据转换为Date类型
     * @param localDateTime 输入localDateTime数据
     * @return Date类型数据
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

}
